package logic

import (
	"Junior_two/subject/goods/goodsrpc/model"
	"context"
	"net/http"
	"strconv"

	"Junior_two/subject/goods/goodsrpc/internal/svc"
	"Junior_two/subject/goods/goodsrpc/pb/goodRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *goodRpc.AddRequest) (*goodRpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	p, _ := strconv.ParseFloat(in.Price, 64)
	err := model.Insert(&model.Goods{
		Name:   in.Name,
		Price:  p,
		TypeId: in.TypeId,
		Status: in.Status,
		Img:    in.Images,
		Desc:   in.Desc,
		Num:    in.Number,
	})
	if err != nil {
		return &goodRpc.AddResponse{
			Msg:  err.Error(),
			Code: http.StatusAccepted,
		}, nil
	}
	return &goodRpc.AddResponse{
		Msg:  "商品添加成功",
		Code: http.StatusOK,
	}, nil
}
